package com.ldw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldw.mapper.TagMapper;
import com.ldw.pojo.Tag;
import com.ldw.service.TagService;
import com.ldw.vo.Result;
import com.ldw.vo.TageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<TageVo> queryByArticleId(Long id) {
        List<Tag> tagList = tagMapper.queryByArticleId(id);
        return copyList(tagList);
    }

    @Override
    public Result hotTage(int limit) {
        List<Long> tags = tagMapper.hotTage(limit);
        if (CollectionUtils.isEmpty(tags)) {
            return Result.success(Collections.emptyList());
        }
        List<Tag> tagList = tagMapper.queryHotTageById(tags);
        return Result.success(tagList);
    }

    @Override
    public Result tags() {
        List<Tag> tags = tagMapper.selectList(null);
        return Result.success(copyList(tags));
    }

    @Override
    public Result tagById(Long id) {
        return Result.success(copyTag(tagMapper.selectById(id)));
    }

    public List<TageVo> copyList(List<Tag> tagList) {
        List<TageVo> tageVoyList = new ArrayList<>();
        for (Tag tag : tagList) {
            tageVoyList.add(copyTag(tag));
        }
        return tageVoyList;
    }

    public TageVo copyTag(Tag tag) {
        TageVo tageVo = new TageVo();
        BeanUtils.copyProperties(tag, tageVo);
        return tageVo;
    }
}
